// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import GameDataController from "../../Controller/GameDataController";

const { ccclass, property } = cc._decorator;

//当前界面状态
enum SceneState {
    //标题移入
    TITLE_MOVING,
    //标题停止并且显示点击进入游戏进入文字
    TITLE_STOP,
    //正在进入游戏
    ENTERING_GAME
}

@ccclass
export default class EnterScene extends cc.Component {

    @property(cc.Node)
    enterTitleNode: cc.Node = null;

    @property(cc.Label)
    tipLabel:cc.Label = null;

    curTitleState: SceneState = SceneState.TITLE_MOVING;

    start() {
        //设置点击监听
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouch, this);
        //标题移入
        this.moveTitleIn();
    }

    moveTitleIn() {
        let callBack = cc.callFunc(this.onMoveTitleEnd, this);
        this.enterTitleNode.runAction(cc.sequence(cc.moveTo(3, cc.v2(0, 0)), callBack));
    }

    onMoveTitleEnd(){
        //展示下方提示点击进入的文字
        this.tipLabel.node.active = true;
        this.curTitleState = SceneState.TITLE_STOP;
    }

    onTouch() {
        switch (this.curTitleState) {
            case SceneState.TITLE_MOVING: {
                //在标题移入的时候点击， 进入TITLESTOP状态，播放进入bgm 等登灯等灯等灯
                this.enterTitleNode.setPosition(0, 0);
                this.enterTitleNode.stopAllActions();
                this.onMoveTitleEnd();
                break;
            }
            case SceneState.TITLE_STOP: {
                //点击确认进入游戏，等待一会儿自动进入第一关关卡信息界面
                this.tipLabel.node.active = true;
                this.tipLabel.node.runAction(cc.repeatForever(cc.sequence(cc.fadeOut(0.2), cc.fadeIn(0.2))));
                this.curTitleState = SceneState.ENTERING_GAME;

                let enterGameCallFunc = cc.callFunc(this.enterGame, this);
                this.node.runAction(cc.sequence(cc.delayTime(5), enterGameCallFunc));
                GameDataController.getInstance().initNewGame();
                break;
            }
            case SceneState.ENTERING_GAME: {
                //正在进入游戏了，点击无反应，等计时器走完进入第一关关卡信息界面

                break;
            }
            default: {

            }
        }
    }

    enterGame(){
        cc.director.loadScene("scene/PreLevelScene");
    }
}
